package com.gxa.uhome.controller;

import com.gxa.uhome.pojo.Material;
import com.gxa.uhome.service.MaterialService;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: XHZ
 * @Date: 10:50 2021/6/8
 */
@RequestMapping("/material")
@RestController
@Api(tags = "Material物料模块", produces = "application/json;charset=utf-8")
@CrossOrigin
public class MaterialController {
    @Resource
    MaterialService materialService;

    @PostMapping("/add")
    @ApiOperation(value = "添加物料", httpMethod = "POST")
    @Transactional
    public JsonResult addMaterial(@RequestBody Material material) {
        Integer insert = materialService.insert(material);
        JsonResult jsonResult = new JsonResult();
        if (insert == 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }

    @PostMapping("/del")
    @ApiOperation(value = "根据物料名称删除物料", httpMethod = "POST")
    @ApiImplicitParams(@ApiImplicitParam(name = "name", value = "物料名称", dataType = "string", paramType = "query"))
    public JsonResult deleteByName(@RequestParam(value = "name") String name) {
        Integer delete = materialService.deleteByName(name);
        JsonResult jsonResult = new JsonResult();
        if (delete == 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改物料信息", httpMethod = "POST")
    public JsonResult updateMaterial(@RequestBody Material material) {
        Integer update = materialService.update(material);
        JsonResult jsonResult = new JsonResult();
        if (update == 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @GetMapping("/like")
    @ApiOperation(value = "物料模糊搜索可选站点分类", httpMethod = "GET")
    @ApiImplicitParams(@ApiImplicitParam(name = "name", value = "物料名称", dataType = "string", paramType = "query"))
    public JsonResult selectLikeByPage(@RequestParam(value = "name",required = false) String name,
                                       @RequestParam(value = "stationName", required = false) String stationName,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "size", defaultValue = "8") Integer size) {
        List<Material> materials = materialService.selectLikeByPage(name, stationName, (page - 1) * size, size);
        JsonResult jsonResult = new JsonResult();
        if (materials != null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(materials);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有物料信息可选站点分类", httpMethod = "GET")
    public JsonResult selectMaterialByPage(@RequestParam(value = "站点名可不填", required = false) String stationName,
                                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                                           @RequestParam(value = "size", defaultValue = "8") Integer size) {
        List<Material> materials = materialService.selectByPage(stationName, (page - 1) * size, size);
        JsonResult jsonResult = new JsonResult();
        if (materials != null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(materials);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

}
